﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Windows;
using System.ComponentModel;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Data.SqlClient;
using System.Data.OleDb;
using System.Data;
using System.Globalization;

namespace QLBDQG
{
    /// <summary>
    /// Interaction logic for DangNhap.xaml
    /// </summary>
    /// 
    public partial class DangNhap : Window
    {
        public SqlConnection m_SCN;
        SqlCommand mySqlCommand;
        SqlDataReader mySqlDataReader;
        public DangNhap()
        {
                m_SCN = new SqlConnection();
                try
                {
                    // Open the connection
                    string path = System.IO.Directory.GetCurrentDirectory();
                    
                    m_SCN.ConnectionString = @"Data Source=.\SQLEXPRESS;AttachDbFilename=" + path +
                        @"\Data\QLBDQG.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True";
                    m_SCN.Open();
                }
                catch (SqlException ex)
                {
                    MessageBox.Show(ex.Message.ToString());
                    MessageBox.Show("Lỗi kết nối CSDL", "ERROR!");
                }
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {

            mySqlCommand = new SqlCommand("DANGNHAP", m_SCN);
            mySqlCommand.CommandType = CommandType.StoredProcedure;
            mySqlCommand.Parameters.Add(new SqlParameter("@TenDangNhap", SqlDbType.VarChar, 50));
            mySqlCommand.Parameters["@TenDangNhap"].Value = textBox1.Text;
            mySqlDataReader = mySqlCommand.ExecuteReader();
               if(mySqlDataReader.Read())
               {
                  if(mySqlDataReader["MaKhau"].ToString()==passwordBox1.Password)
                  {
                      mySqlCommand.Dispose();
                      mySqlDataReader.Dispose();
                      QLSAN m_QLSAN = new QLSAN(m_SCN);
                      m_QLSAN.Show();


                      this.Close();
                  }
                  else
                  {
                      MessageBox.Show("Mat khau sai");
                  }
               }
            else
               {
                   MessageBox.Show("Khong ton tai ID");
                   
                   
               }
               mySqlDataReader.Dispose();
           
        }

        private void button2_Click(object sender, RoutedEventArgs e)
        {
            if (MessageBox.Show("Ban thuc su muon thoat", "Thong Bao", MessageBoxButton.YesNo) == MessageBoxResult.Yes)
            {
                this.Close();
            }
            
            
        }
    }
}
